package com.google.codejam.round1B2013.osmos;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Osmos {

    public static void main(String[] args) {
        String inputFile = args[0];
        String outputFile = args[1];
        readAndSolve(inputFile, outputFile);
    }

    private static int getResult(int sum, List<Integer> level, int index) {
        if(index>=level.size()){
            return 0;
        }
        if(sum>level.get(index)){
            return getResult(sum+level.get(index), level, index+1);
        }
        else{
                int add=Integer.MAX_VALUE;
                if(sum>1){
                    add = getResult(sum+sum-1, level, index)+1;
                }
                int remove = level.size()-index;
                return Math.min(add, remove);
        }
    }

    public static String readAndSolve(String inFileName, String outFileName) {
        File file = new File(inFileName);
        BufferedReader bufferedReader = null;
        try {
            bufferedReader = new BufferedReader(new FileReader(file));
            FileWriter fileWriter = new FileWriter(outFileName, true);
            String readLine = null;
            int currentTestCaseIndex = 1;
            int line = 0;
            int A=0, N=0;
            boolean readA=true;

            while ((readLine = bufferedReader.readLine()) != null) {
                if (readLine.trim().length() <= 0) {
                    continue;
                }
                if (line == 0) {
                    // ignore read the test case
                    line++;
                    continue;
                }
                List<Integer> level = new ArrayList<Integer>();
                String[] interval = readLine.split(" ");
                if(readA){
                    A = Integer.valueOf(interval[0]);
                    N = Integer.valueOf(interval[1]);
                    readA=false;
                    continue;
                }
                else{
                    //read N number
                    for(int i=0; i < interval.length; i++){
                        level.add(Integer.valueOf(interval[i]));
                    }
                    readA = true;
                }
//                System.out.println(A+","+N+","+level);
                Comparator comp = new MyComparator();
                Collections.sort(level, comp);
                int result = getResult(A, level, 0);
//                System.out.println(level);
//                System.out.println("result:"+result);
                fileWriter.append("Case #" + currentTestCaseIndex + ": " + result);
                fileWriter.append("\n");
                currentTestCaseIndex++;
            }
            fileWriter.flush();
            fileWriter.close();
            bufferedReader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
